// Package __linklist 19_remove_nth_node
// @Title 2_linklist
// @Description  19. 删除链表的倒数第 N 个结点
// @Author  == 琳琳也叫王木木 ==  2021/9/2 14:07
// @Create  2021/9/2 14:07
package __linklist

// RemoveNthFromEnd 删除链表倒数第k个节点
func RemoveNthFromEnd(head *ListNode, n int) *ListNode {
	dummy := &ListNode{0, head}
	first, second := head, dummy
	for i := 0; i < n; i++ {
		first = first.Next
	}
	for ; first != nil; first = first.Next {
		second = second.Next
	}
	second.Next = second.Next.Next
	return dummy.Next
}
